<template>
    <div>授权中。。。</div>
</template>

<script>
import constant from "@/utils/constant.js";
// 引用API文件
import http from "@/utils/http.js";
export default {
  data() {
    return {
      token: "",
      user_info: {}
    };
  },
  computed: {},
  created() {
      console.log('author created');
      this.authorlogin();
  },
  activated() {
      console.log('author activated');
      this.authorlogin();
  },
  methods: {
    // login() {
    //   let url = this.webUrl + "/Wap/User/info";
    //   // 通过cookie中保存的token 获取用户信息
    //   this.$http.get(url).then(response => {
    //     response = response.body;
    //     if (response) {
    //       // 保存用户登录状态(Vuex)
    //       this.$store.commit("user", response);
    //       setTimeout(() => {
    //         this.goBeforeLoginUrl(); // 页面恢复(进入用户一开始请求的页面)
    //       }, 2000);
    //     } else {
    //       this.$alert("服务器撸猫去惹 :(", "wrong");
    //       if (holdno.cookie.get("user")) {
    //         // 跳转到微信授权页面
    //         window.location.href = this.webUrl + "/Wap/User/getOpenid";
    //       }
    //     }
    //   });
    // }
    userinfo() {
      http.get(this, {
        url: "ucenter/user_info",
        data: {
          token: localStorage.getItem("user_token")
        },
        dataType: "json",
        success: function(data) {
          console.log("用户信息:", data);
          if (data.status == 1) {
            this.user_info = data.info;
            localStorage.setItem("user_name", data.info.username);
            localStorage.setItem("user_head", data.info.headimgurl);

            if (!this.user_info.mobile) {
              this.$router.push({
                path: "/login/bindmobile"
              });
            } else {
              this.$router.push({
                path: "/center"
              });
            }
          } else {
          }
        }
      });
    },
    authorlogin() {
      //判断当前的url有没有token参数,如果不存在那就跳转到微信授权的url
      //就是前面说的ReturnGetCodeUrl方法
      let token = this.$route.query.token;
      console.log("author获取参数token:" + token);
      if (token) {
        localStorage.setItem("user_token", token);
        this.token = localStorage.getItem("user_token");
      } else {
        localStorage.removeItem("user_token");
      }

      console.log("author的url:" + window.location.href);
      console.log("author获取token:" + token);
      console.log("author路由", this.$route);

      console.log(this.token);
      // 检测会员有没有登录
      if (!this.token) {
        let ua = window.navigator.userAgent.toLowerCase();
        if (ua.match(/MicroMessenger/i) == "micromessenger") {
          // 跳转到微信授权页面
        }

        localStorage.removeItem("user_name");
        localStorage.removeItem("user_head");
        localStorage.removeItem("user_type");

        let invite_code = localStorage.getItem("invite_code");
        
        console.log("author跳转地址:" + constant.api_url +
          "weixin/get_wx_userinfo?invite_code=" +
          invite_code +
          "&redirect_uri=" +
          ("http://" + window.location.host));
        window.location.href =
          constant.api_url +
          "weixin/get_wx_userinfo?invite_code=" +
          invite_code +
          "&redirect_uri=" +
          ("http://" + window.location.host);
      } else {
        // 如果有token 但是vuex中没有用户登录信息则做登录操作encodeURIComponent
        // this.login();
        this.userinfo();
      }
    }
  },
  watch: {
    $route(to, from) {
      console.log('author watch');
      // to表示的是你要去的那个组件，from 表示的是你从哪个组件过来的，它们是两个对象，你可以把它打印出来，它们也有一个param 属性
      console.log(to);
      console.log(from);
      if (to.path == "/author/author") {
        this.authorlogin();
      }
    }
  },

  components: {},

  mounted: function() {}
};
</script>
<style lang='scss' scoped>
</style>
